package com.yang.SpingBoot.model;

import com.yang.SpingBoot.dao.GuBaMapper;
import com.yang.SpingBoot.exception.HttpRequestFailedException;
import com.yang.SpingBoot.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 *
 * @author 杨建全
 * @Date 2018/12/10 12:15
 */
public class CrawerTask implements Runnable{

    private Logger logger = LoggerFactory.getLogger(CrawerTask.class);
    private String company;
    private int pageNum;
    private String code;
    private GuBaMapper guBaMapper;

    public CrawerTask(String company, int pageNum, String code, GuBaMapper guBaMapper) {
        this.company = company;
        this.pageNum = pageNum;
        this.code = code;
        this.guBaMapper = guBaMapper;
    }

    @Override
    public void run() {
        while (pageNum!=0){

            String url = "http://guba.eastmoney.com/list,"+code+",f_"+pageNum+".html";

            try {
                String html = HttpUtil.get(url, null);
                logger.info("抓取{"+company+"}的第{"+pageNum+"}页数据");

                if (StringUtils.isNotEmpty(html)){
                    List<GuBa> guBaList = parseHtml(html);

                    saveData(guBaList);
                }
            } catch (HttpRequestFailedException e) {
                logger.error("请求异常",e);
            }
            pageNum--;
        }
    }

    private void saveData(List<GuBa> guBaList) {

        for (GuBa guBa : guBaList) {
            try{
                guBaMapper.save(guBa);
            }catch (Exception e){
                logger.error("保存数据异常",e);
            }

        }

    }

    private List<GuBa> parseHtml(String html) {

        Document document = Jsoup.parse(html);

        Element articlelistnew = document.getElementById("articlelistnew");

        Elements articlehs = articlelistnew.select(".articleh");

        List<GuBa> data = new ArrayList<>();

        articlehs.forEach(element -> {
            Elements yd = element.select(".l1");
            Elements pl = element.select(".l2");
            Elements title = element.select(".l3");
            Elements author = element.select(".l4");
            Elements publishTime = element.select(".l6");

            GuBa guBa = new GuBa();
            guBa.setCompany(company);
            guBa.setYd(yd.text());
            guBa.setPl(pl.text());
            guBa.setTitle(title.text());
            guBa.setAuthor(author.text());
            guBa.setPublishtime(publishTime.text());

            data.add(guBa);
        });

        return data;

    }
}
